package com.xingying.travel.controller;


import com.xingying.travel.common.PageResult;
import com.xingying.travel.common.Result;
import com.xingying.travel.common.StatusCode;
import com.xingying.travel.config.SessionListener;
import com.xingying.travel.pojo.Article;
import com.xingying.travel.pojo.User;
import com.xingying.travel.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.scheduling.support.SimpleTriggerContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@CrossOrigin
@RequestMapping("/article")
public class ArticleController
{

    @Autowired
    private ArticleService articleService;

    /**
     * 增加
     * @param article
     */
    @ResponseBody
    @RequestMapping(method= RequestMethod.POST)
    public Result add(Article article, HttpSession session){
        User us= (User) session.getAttribute("user");
        article.setCreateId(us.getId());
        article.setCreateBy(us.getName());
        article.setCreateTime(new Date());
        articleService.add(article);
        return new Result(true, StatusCode.OK,"增加成功");
    }



    /**
     * 分页+多条件查询
     * @param searchMap 查询条件封装
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @ResponseBody
    @RequestMapping(value="/search/{page}/{size}",method=RequestMethod.POST)
    public Result findSearch(@RequestBody Map searchMap , @PathVariable int page, @PathVariable int size){
        Page<Article> pageList = articleService.findSearch(searchMap, page, size);
        return  new Result(true,StatusCode.OK,"查询成功",  new PageResult<Article>(pageList.getTotalElements(), pageList.getContent()) );
    }


    /**
     * 查询全部数据
     *
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    @ResponseBody
    public Result findAll() {
        return new Result(true, StatusCode.OK, "查询成功", articleService.findAll());
    }


    /**
     * 删除
     * @param id
     */
    @ResponseBody
    @RequestMapping(value="/{id}",method= RequestMethod.DELETE)
    public Result delete(@PathVariable String id ){
        articleService.deleteById(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }


    /**
     * 审核
     * @param id
     */
    @ResponseBody
    @RequestMapping(value="/{id}",method= RequestMethod.PUT)
    public Result update(@PathVariable String id ){

        articleService.update(id);
        return new Result(true,StatusCode.OK,"修改成功");
    }



    /**
     * 增加
     * @param
     */
    @ResponseBody
    @RequestMapping(value = "/count",method= RequestMethod.GET)
    public Result count(){
           int onlineCount =   UserController.getOnlineCount();
           int  count = articleService.findCount();
           Map<String,Integer> map = new HashMap<>();
           map.put("onlineCount",onlineCount);
           map.put("count",count);
        return new Result(true, StatusCode.OK,"查询成功",map);
    }



    /**
     * 跳转页面
     * @return
     */
    @RequestMapping(value = "/articleList")
    public String articleList(){
        return "admin/articlemanage/articleList";
    }







}
